######################################################################################
## TITLE:	  Compulsory voting rules, reluctant voters and proximity			            ##
## AUTHORS:	Dassonneville, Feitosa, Hooghe, Lau & Stiers 					                  ##
## PURPOSE:	Replication of main results, matching Swiss data logit (CV strict)    	##
######################################################################################


###############################
####### Setting Up R      #####
###############################

#### Installing Packages ####
## Note: You only have to install packages when using them for the first time on your computer

install.packages("RcppArmadillo")
install.packages("MatchIt")
install.packages("optmatch")

#### Loading Libraries ####
## Note: You have to install libraries every time you open R. 

library(foreign)
library(MatchIt)

###############################
####### Loading Data      #####
###############################


#### Loading Data ####

setwd("/Users/ruthdassonneville/Box Sync/Compulsory Voting and correct voting/Replication materials dataverse")
data1 <- read.dta("Switzerland_coded.dta")
dim(data1)
names(data1)
head(data1)



#######################################################
####### Matching - Create matched dataset   		  #####
#######################################################

### Subsetting the Datasets by year ####
myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "relpract")
votedata <- data1[myvars]

votedata71 <-votedata[votedata[, "year"] == 1971,]
votedata95 <-votedata[votedata[, "year"] == 1995,]
votedata99 <-votedata[votedata[, "year"] == 1999,]
votedata03 <-votedata[votedata[, "year"] == 2003,]
votedata07 <-votedata[votedata[, "year"] == 2007,]
votedata11 <-votedata[votedata[, "year"] == 2011,]


votedata71 <- na.omit(votedata71)
votedata95 <- na.omit(votedata95)
votedata99 <- na.omit(votedata99)
votedata03 <- na.omit(votedata03)
votedata07 <- na.omit(votedata07)
votedata11 <- na.omit(votedata11)


### 1971

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata71, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.71 <- match.data(m.out1) 
  write.dta(m.vote.71, "m.votestrict71-caliper2.dta") 

### 1995

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata95, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.95 <- match.data(m.out1) 
  write.dta(m.vote.95, "m.votestrict95-caliper2.dta") 

### 1999

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata99, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.99 <- match.data(m.out1) 
  write.dta(m.vote.99, "m.votestrict99-caliper2.dta") 

### 2003

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata03, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.03 <- match.data(m.out1) 
  write.dta(m.vote.03, "m.votestrict03-caliper2.dta") 

### 2007

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata07, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.07 <- match.data(m.out1) 
  write.dta(m.vote.07, "m.votestrict07-caliper2.dta") 

### 2011

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata11, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.11 <- match.data(m.out1) 
  write.dta(m.vote.11, "m.votestrict11-caliper2.dta") 





